package com.km.noh.weatherinfo.async;

import java.util.HashMap;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.widget.ImageView;

import com.km.noh.weatherinfo.R;

public class ImageAsync extends AsyncTask<String, Integer, Bitmap> {
	private ImageView iv;
	private String url;
	private int defaultImg = -1;
	private static int asyncCount=0;
	
	@SuppressWarnings("unchecked")
	private static HashMap<String, Bitmap>[] cachedImage = new HashMap[4];
	
	static {
		for (int i=0;i<cachedImage.length;i++) {
			if (cachedImage[i]==null) { 
				cachedImage[i] = new HashMap<String, Bitmap>();
			}
		}
	}
	
	public ImageAsync(String url, ImageView iv) {
		this.url = url;
		this.iv = iv;
//		this.defaultImg = R.drawable.book_default_ico;
		init();
	}
	
	public ImageAsync(String url, ImageView iv, int defaultImg) {
		this.url = url;
		this.iv = iv;
		this.defaultImg = defaultImg;
		init();
	}
	
	private void init() {
		if (containsKey(url)) {
			iv.setImageBitmap(getData(url));
		} else {
			if(asyncCount<100){
				asyncCount++;
				execute();
			}				
		}
	}
	
	private static boolean containsKey(String url) {
		for (int i=0;i<cachedImage.length;i++) {
			if (cachedImage[i].containsKey(url)) return true;
		}
		return false;
	}
	
	private static Bitmap getData(String url) {
		for (int i=0;i<cachedImage.length;i++) {
			if (cachedImage[i].containsKey(url)) return cachedImage[i].get(url);
		}
		return null;
	}
	
	
	public static void addBitmap(String url, Bitmap bitmap) {
		cachedImage[0].put(url, bitmap);
		if (cachedImage[0].size()>=50) {
			HashMap<String, Bitmap> tmp = cachedImage[cachedImage.length-1];
			for (int i=cachedImage.length-1;i>0;i--) {
				cachedImage[i] = cachedImage[i-1]; 
			}
			cachedImage[0] = new HashMap<String, Bitmap>();
			tmp.clear();
		}
	}
	
	public static void clear() {
		for (int i=0;i<cachedImage.length;i++) {
			cachedImage[i].clear();
		}
	}
//	public static void clear(HashMap<String, Bitmap> cashMap) {
//		Util.log("heapSize pre_clear ==>>"+Debug.getNativeHeapSize()+"/"+ Debug.getNativeHeapFreeSize()+"/"+ Debug.getNativeHeapAllocatedSize());
//		if(cashMap ==null){
//			for (int i=0;i<cachedImage.length;i++) {
//				Iterator<String> et =cachedImage[i].keySet().iterator();
//					while (et.hasNext()) {
//						
//						Bitmap bm = cachedImage[i].get(et.next());
//						Util.log("bm  ==>>"+bm);					
//						bm.recycle();
//						
//					}
//					cachedImage[i].clear();
//			}
//		}else{
//			Iterator<String> et =cashMap.keySet().iterator();
//			while (et.hasNext()) {
//				
//				Bitmap bm = cashMap.get(et.next());
//				Util.log("bm  ==>>"+bm);					
//				bm.recycle();
//			}
//			cashMap.clear();
//		}		
//		Util.log("heapSize after_clear ==>>"+Debug.getNativeHeapSize()+"/"+ Debug.getNativeHeapFreeSize()+"/"+ Debug.getNativeHeapAllocatedSize());
//	}

	@Override
	protected Bitmap doInBackground(String... arg0) {
		if (url==null || url.length()<1) return null;
		Bitmap bm = null;
		try {
			if (containsKey(url)) return getData(url);
//			bm = Util.getBitmapImage(url, false);
		}catch (Exception e) {
//			Util.logError(e);
		}
		return bm; 
	}

	@Override
	protected void onPostExecute(Bitmap tempData) {
		asyncCount--;
		if (tempData==null) {
			if (defaultImg>-1) {
				iv.setImageResource(defaultImg);
			} else {
				iv.setImageBitmap(null);
			}
		} else {
			addBitmap(url, tempData);
			iv.setImageBitmap(tempData);
//			iv.setScaleType(ImageView.ScaleType.FIT_XY);
//			iv.setBackgroundDrawable(new BitmapDrawable(tempData));
		}
	}
}
